<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Common\CommonController;
use Illuminate\Http\Request;
use App\Model\Common\Issue;
use Validator;

class IssueController extends CommonController
{
    public function add(Request $request)
    {
        $data = $request->all();

        $validator = Validator::make($data, [
            'question' => 'bail|required',
            'email' => 'required|email',
        ]);

        if ($validator->fails()) {
            return $this->output(10005, $validator->errors());
        }

        if (empty($data['project_id']) && empty($data['api_id'])) {
            return $this->output(10005);
        }
        $data['question'] = nl2br($data['question'], true);
        $data['question'] = preg_replace('/(<br\s?\/?>\s+)+/', '<br />', $data['question']);
        $Issue = new Issue();

        $re = $Issue->fillData($data)->save();

        if ($re) {
            return $this->output(0, '反馈成功');
        } else {
            return $this->output(10001, '反馈失败');
        }
    }
}
